﻿using BaseDll;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;

namespace OtherDevice
{
    [Description("ATQ气密仪")]
    public class Air_ATQ: AT_Base
    {
        public Air_ATQ( string strName):base(strName)
        {
            controlFrom = new Form_气密仪();
        }
        double d_大泄露 = 0;
        double d_小泄露 = 0;
        bool b_Reflush = false;
        object obj = new object();
        public override void InitDeal()
        {
            m_SerialPort.DataReceived += M_SerialPort_DataReceived;
        }
        private void M_SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] byte_Result =null;
                ReceiveData(ref byte_Result, 3000);
                string strResult = Encoding.Default.GetString(byte_Result);
                string[] list_Result = strResult.Split(':');
                if (list_Result.Length < 4)
                    return;
                string str_Value = list_Result[1];
                string[] list_Value = str_Value.Split(' ');
                d_大泄露 =Convert.ToDouble(list_Value[0]);
                list_Value=list_Result[3].Split(' ');
                d_小泄露 = Convert.ToDouble(list_Value[0]);
                b_Reflush = true;
                return;

            }
            catch (Exception)
            {

            }
        }

        public override void Function_气密测试启动()
        {
            b_Reflush = false;
        }
        public override int Fucntion_获取气密测试(ref double d_大泄露, ref double d_小泄露, int n_TimeOut = 2000)
        {
            lock (obj)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                while (true)
                {
                    if (n_TimeOut > 0 && stopwatch.ElapsedMilliseconds > n_TimeOut)
                    {
                        return -2;
                    }
                    if (b_Reflush)
                    {
                        d_大泄露 = this.d_大泄露;
                        d_小泄露 = this.d_小泄露;
                        return 0;
                    }
                    Thread.Sleep(5);
                }


            }
        }

    }
}